#include<bits/stdc++.h>
#define ll long long
using namespace std;
template <typename T> inline void read(T &x) {
    x = 0; char ch = getchar(); int f = 1;
    while (!isdigit(ch) && ch ^ '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); x *= f;
}
inline void init(){
	freopen("tsuki.in","r",stdin);
	freopen("tsuki.out","w",stdout);
}
const int Maxn=1005;
const ll Mod2=147744151;
int n,m,p;
int a[Maxn][Maxn];
int inv[Maxn*Maxn];
inline void solve20(){
	int q,ans=0;
	read(q);
	for(int k=1;k<=q;k++){
		int x,y,s,t;
		read(x);read(y);read(s);read(t);
		int ans1=1;
		for(int i=x;i<=s;i++)
			for(int j=y;j<=t;j++)
				ans1=1ll*ans1*1ll*a[i][j]%(1ll*p);
		ans=(ans+(1ll*k^ans1)%Mod2)%Mod2;
	}
	printf("%d",ans);
}
inline int ksm(int a,int b){
	int res=1;
	while(b){
		if(b&1)res=1ll*res*a%p;
		a=1ll*a*a%p;
		b>>=1;
	}
	return res;
}
int sum[Maxn][Maxn];
inline void solve6_17(){
	int q,ans=0;
	read(q);
	for(int k=1;k<=q;k++){
		int x,y,s,t;
		read(x);read(y);read(s);read(t);
		x--;y--;
		int ans1=1ll*sum[s][t]*sum[x][y]%p*inv[sum[x][t]]%p*inv[sum[s][y]]%p;
		ans=(ans+(1ll*k^ans1)%Mod2)%Mod2;
	}
	printf("%d",ans);
}
inline void solve18_21(){
	int q,ans=0;
	read(q);
	for(int k=1;k<=q;k++){
		int x,y,s,t;
		read(x);read(y);read(s);read(t);
		x--;y--;
		int ans1=1ll*sum[s][t]*sum[x][y]%p*ksm(sum[x][t],p-2)%p*ksm(sum[s][y],p-2)%p;
		ans=(ans+(1ll*k^ans1)%Mod2)%Mod2;
	}
	printf("%d",ans);
}
int main(){
	init();
	read(n);read(m);read(p);
	if(p<=1e6+5){
		inv[1]=1;
		for(int i=2;i<=p;i++)inv[i]=1ll*inv[p%i]*(p-p/i)%p;
	}
	for(int i=0;i<=n;i++)sum[i][0]=1;
	for(int i=0;i<=m;i++)sum[0][i]=1;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++){
			read(a[i][j]);
			if(p==998244353)sum[i][j]=1ll*sum[i-1][j]*sum[i][j-1]%p*ksm(sum[i-1][j-1],p-2)%p*a[i][j]%p;
			else sum[i][j]=1ll*sum[i-1][j]*sum[i][j-1]%p*inv[sum[i-1][j-1]]%p*a[i][j]%p;
		}
	if(max(n,m)<=205)solve20();
	else{
		if(p==998244353)solve18_21();
		if(p<=1e6+5)solve6_17();
	}
	return 0;
}

